
package com.sc.nft.entity.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sc.nft.entity.*;
import com.sc.nft.enums.SaasTaskParticipatorEnum;
import com.sc.nft.enums.SaasTaskRestEnum;
import com.sc.nft.enums.SaasTaskTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class SaasTaskWebDetailsVO {
    @ApiModelProperty(value = "任务id")
    private Long id;

    @ApiModelProperty(value = "任务重置类型")
    private SaasTaskRestEnum taskRest;

    @ApiModelProperty(value = "任务名字")
    private String taskName;

    @ApiModelProperty(value = "任务icon")
    private String taskIcon;

    @ApiModelProperty(value = "是否是首次参与,目前只用到了熔炼,意思就是是不是从注册开始第一次熔炼")
    private Boolean firstParticipation;

    @ApiModelProperty(value = "任务剩余数量")
    private Integer taskResidueSum;

    @ApiModelProperty(value = "开始时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date taskStart;

    @ApiModelProperty(value = "结束时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date taskEnd;

    @ApiModelProperty(value = "任务参与者 1:无门槛  2:持有指定藏品")
    private SaasTaskParticipatorEnum taskParticipator;

    @ApiModelProperty(value = "任务进度")
    private SaasTaskScheduleVO saasTaskScheduleVO;

    @ApiModelProperty(value = "任务每个人最大完成上限")
    private Integer taskEveryoneSum;

    @ApiModelProperty(value = "完成时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date fulfilTime;

    @ApiModelProperty(value = "领取时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date getTime;

    @ApiModelProperty(value = "任务已完成次数")
    private Integer offTheStocksSum;

    @ApiModelProperty(value = "任务奖励排名")
    private String saasTaskAwardRanking;

    @ApiModelProperty(value = "任务持有nft条件")
    private List<SaasTaskConditionVO> taskConditions = new ArrayList<>();

    @ApiModelProperty("完成任务模板")
    private SaasTaskMeetConditionsWebVO saasTaskMeetConditionsWeb;

    @ApiModelProperty("完成任务模板中 商品是否超过了三个")
    private Boolean templateOverlength= Boolean.FALSE;

    @ApiModelProperty(value = "任务奖励")
    private List<SaasTaskAwardVO> saasTaskAwards = new ArrayList<>();


    public void basics(SaasTask saasTask, Integer sum) {
        this.id = saasTask.getId();
        this.taskResidueSum = saasTask.getTaskResidueSum();
        this.taskParticipator = saasTask.getTaskParticipator();
        this.taskEveryoneSum = saasTask.getTaskEveryoneSum();
        this.taskName = saasTask.getTaskName();
        this.taskIcon = saasTask.getTaskIcon();
        this.taskRest = saasTask.getTaskRest();
        this.taskStart = saasTask.getTaskStart();
        this.taskEnd = saasTask.getTaskEnd();
        this.firstParticipation = saasTask.getFirstParticipation();
        // 任务已完成次数
        this.offTheStocksSum = sum;
    }

    /**
     * 任务参与
     *
     * @param saasTaskMeetConditions
     */
    public void meetConditions(SaasTaskMeetConditions saasTaskMeetConditions) {
        SaasTaskMeetConditionsWebVO saasTaskMeetConditionsWebVO = new SaasTaskMeetConditionsWebVO();
        saasTaskMeetConditionsWebVO.setTaskType(saasTaskMeetConditions.getTaskType());
        saasTaskMeetConditionsWebVO.setVoluntarySum(saasTaskMeetConditions.getVoluntarySum());
        saasTaskMeetConditionsWebVO.setSchedule(saasTaskMeetConditions.getSchedule());
        saasTaskMeetConditionsWebVO.setId(saasTaskMeetConditions.getId());
        this.saasTaskMeetConditionsWeb = saasTaskMeetConditionsWebVO;
    }


    public void saasTaskBuys(List<SaasTaskDetailsCollectionVO> data) {
        List<SaasTaskBuyVO> list = new ArrayList<>();
        data.forEach(saasTaskDetailsCollectionVO -> {
            SaasTaskBuyVO saasTaskBuyVO = new SaasTaskBuyVO();
            saasTaskBuyVO.setCreationContent(saasTaskDetailsCollectionVO.getCreationContent());
            saasTaskBuyVO.setFirstTitle(saasTaskDetailsCollectionVO.getConditionName());
            list.add(saasTaskBuyVO);
        });
        this.saasTaskMeetConditionsWeb.setSaasTaskBuys(list);
    }

    public List<Long> byTaskIdLeftDigCollectionId() {
        return this.saasTaskAwards.stream().map(SaasTaskAwardVO::getCollectionId).collect(Collectors.toList());
    }

    /**
     * 排序奖励
     *
     * @param rankingData
     */
    public void rewardRanking(List<UserCollection> rankingData) {
        this.saasTaskAwards.sort(Comparator.comparing(SaasTaskAwardVO::getWeight).reversed());
        List<Long> collestionIds = rankingData.stream().map(UserCollection::getCollectionId).collect(Collectors.toList());
        // 倒序排序后，最大的在上面，开始循环比对，如果当前的满足,则对他进行满足,结束循环
        for (SaasTaskAwardVO saasTaskAward : this.saasTaskAwards) {
            if (saasTaskAward.getCollectionId()==null) {
                saasTaskAward.setAvailableReward(Boolean.TRUE);
                break;
            }
            if (collestionIds.contains(saasTaskAward.getCollectionId())) {
                saasTaskAward.setAvailableReward(Boolean.TRUE);
                break;
            }
        }
        this.saasTaskAwards.sort(Comparator.comparing(SaasTaskAwardVO::getAvailableReward).reversed().thenComparing(Comparator.comparing(SaasTaskAwardVO::getWeight).reversed()));
    }


    public void userTemplateTask(SaasTaskMeetConditions saasTaskMeetConditions, SaasUserTask saasUserTask) {
        this.saasTaskScheduleVO = BeanUtil.copyProperties(saasTaskMeetConditions, SaasTaskScheduleVO.class);
        if (SaasTaskTypeEnum.OPEN_WALLET_FOR_THE_FIRST_TIME == saasTaskMeetConditions.getTaskType()||SaasTaskTypeEnum.BUSINESS_CARD_BUY == saasTaskMeetConditions.getTaskType()) {
            this.saasTaskScheduleVO.setSchedule(Convert.toBigDecimal(1));
        }
        if (BeanUtil.isNotEmpty(saasUserTask)) {
            if (this.saasTaskScheduleVO.getTaskType() == SaasTaskTypeEnum.FIRST_TIME_OPEN_WALLET_INVITATION
                    || this.saasTaskScheduleVO.getTaskType() == SaasTaskTypeEnum.INVITATION_TO_PURCHASE_SPECIFIED_COLLECTIONS
                    ||this.saasTaskScheduleVO.getTaskType() == SaasTaskTypeEnum.INVITE_NEW_USERS_TO_MAKE_THEIR_FIRST_PURCHASE
                    ||this.saasTaskScheduleVO.getTaskType()==SaasTaskTypeEnum.PASSIVITY_INVITE_SMELTING_APPOINT_COLLECTION
                    ||this.saasTaskScheduleVO.getTaskType()==SaasTaskTypeEnum.PASSIVITY_INVITE_SMELTING_WILL_COLLECTION ){
                this.saasTaskScheduleVO.setUserSchedule(Convert.toBigDecimal(saasUserTask.getVoluntarySum()));
            }else {
                this.saasTaskScheduleVO.setUserSchedule(saasUserTask.getSchedule());
            }
            this.fulfilTime = saasUserTask.getFulfilTime();
            this.getTime = saasUserTask.getGetTime();
        }
        voluntarySumConvertSchedule();
    }
    public void voluntarySumConvertSchedule() {
        // 当任务类型是FIRST_TIME_OPEN_WALLET_INVITATION和INVITATION_TO_PURCHASE_SPECIFIED_COLLECTIONS时,把voluntarySum字段和schedule字段值互换
        if (this.saasTaskScheduleVO.getTaskType() == SaasTaskTypeEnum.FIRST_TIME_OPEN_WALLET_INVITATION
                || this.saasTaskScheduleVO.getTaskType() == SaasTaskTypeEnum.INVITATION_TO_PURCHASE_SPECIFIED_COLLECTIONS
                ||this.saasTaskScheduleVO.getTaskType() == SaasTaskTypeEnum.INVITE_NEW_USERS_TO_MAKE_THEIR_FIRST_PURCHASE
                ||this.saasTaskScheduleVO.getTaskType()==SaasTaskTypeEnum.PASSIVITY_INVITE_SMELTING_APPOINT_COLLECTION
                ||this.saasTaskScheduleVO.getTaskType()==SaasTaskTypeEnum.PASSIVITY_INVITE_SMELTING_WILL_COLLECTION) {
            BigDecimal temp = new BigDecimal(this.saasTaskScheduleVO.getVoluntarySum()!=null
                    && this.saasTaskScheduleVO.getVoluntarySum()>0?this.saasTaskScheduleVO.getVoluntarySum():0);
            this.saasTaskScheduleVO.setVoluntarySum(Convert.toInt(this.saasTaskScheduleVO.getSchedule()));
            this.saasTaskScheduleVO.setSchedule(temp);
        }
    }
    public void setSaasTaskStageConditions(SaasTaskStageConditions saasTaskStageConditions) {
        if (ObjectUtil.isNotEmpty(saasTaskStageConditions)) {
            this.saasTaskAwardRanking=StrUtil.format("{}-{}名",saasTaskStageConditions.getLowerLimit()+1,saasTaskStageConditions.getUpperLimit());
            return;
        }
        this.saasTaskAwardRanking=StrUtil.format("额度已满");
    }
}
